/**************************************************************************/
/*  Copyright (C) School of Software Engineering of USTC, 2013             
/*                                                                        
/*  FILE NAME: "FolderOpt.c"                                                  
/*  PRINCIPAL AUTHOR: SA12226340(Song Zeyu)                                                                                                          
/*  LANGUAGE: C++/C                                                          
/*  TARGET ENVIRONMENT: Linux                                             
/*  DATE OF FIRST RELEASE: 2013-05-13                                    
/*  DESCRIPTION:Realization mail spam filtering               
/**************************************************************************/

#include "FolderOpt.h"
#include "MyMap.h"
#include "WordSegFolder.h"
#include "common.h"
#include <dirent.h>

#define MaxLine 1024
/*
* 文件夹里文件的路径和名字
*/
struct FileList
{
	char file_path[64];
    struct FileList *next;
};

struct FileList *Folder(char *foldername);
int FileExtract(struct FileList *node);

/*
*训练邮件集的垃圾邮件和正常邮件，得到垃圾库和合法库
*/
void Init_Folder_Map(map<string,int> &spam_map,map<string,int> &pam_map,map<string,int> mapword[])
{
	/*
	*Initialization,include file directory,FILE pointer,buffer
	*/
	struct FileList *SpamFile,*PamFile;
	int spam_file_nodes;                    /*the index file stores into the index mapword,file_nodes means map total */
	int pam_file_nodes;	
	char spamfolder[10] = "spam";
	char pamfolder[10] = "pam";
	
  	SpamFile = Folder(spamfolder);
  	PamFile = Folder(pamfolder);
  	
  	#if 0
  	DisFolderFilesName(SpamFile);
	DisFolderFilesName(PamFile);
  	#endif
  	
  	/*
  	*statistics the word numbers of all SpamFile and insert the word in spam_map<string,int>
  	*/
  	spam_file_nodes = FileExtract(SpamFile,mapword);
  	StatisticsMapWords(spam_map,spam_file_nodes,mapword);
  
    PrintfResult_map(spam_map);    /*printf the word and the word number of spam_map*/ 
    DeleteMMap(spam_file_nodes,mapword);   /*delete memory of mapword[]*/
    

	/*
  	*statistics the word numbers of all PamFile and insert the word in pam_map<string,int>
  	*/
  	pam_file_nodes = FileExtract(PamFile,mapword);
  	StatisticsMapWords(pam_map,pam_file_nodes,mapword);
    
    PrintfResult_map(pam_map);    /*printf the word and the word number of spam_map*/
    DeleteMMap(pam_file_nodes,mapword);   /*delete memory of mapword[]*/
    
}


/*
*将文件夹里的文件以链表的形式存放
*/
struct FileList *Folder(char *foldername)
{
	DIR *directory_pointer;                         //define a folder directory pointer to folder
	struct dirent *entry; 
	struct FileList start,*node;

	if ((directory_pointer=opendir(foldername))==NULL)
	{
		printf("Error opening %s\n",foldername);
		return NULL;
	}
    	
    else
	{
		start.next=NULL;
		node=&start;
		while ((entry=readdir(directory_pointer))!=NULL)
    	{
    		if(strcmp(entry->d_name,".")!=0 && strcmp(entry->d_name,"..")!=0)
    		{
    			char path[50]="./";
				strcat(path,foldername);
				strcat(path,"/");            // relative paths,
				
      			node->next=(struct FileList *)malloc(sizeof(struct FileList));
      			node=node->next;
      			node->next = NULL;
      			strcpy(node->file_path,entry->d_name); //entry->d_name means file name
      			strcat(path,node->file_path);
      			strcpy(node->file_path,path);       //node->file_path means file the absolute path
    		}   		
    	}
		closedir(directory_pointer);
		/*

		*for test,output all file of file_path
		*/
		node=start.next;
		return node;
  	}
}


/*
*对文件夹里的文件进行中文分词提取
*/
int FileExtract(struct FileList *node,map<string,int>mapword[])
{
	char buffer[MaxLine] = {'\0'};
	FILE *fp = NULL;
	int file_nodes = 0;
	while(node)
  	{
  		fp = fopen(node->file_path,"rb");
      	if(fp == NULL)
    	{
        	printf("cannot open this file!\n");
        	continue;
    	}
      	while(!feof(fp))
      	{
			if(fgets(buffer,MaxLine,fp) == NULL)
           		break;
        	buffer[strlen(buffer)-1] = '\0';	
			SegmentSentenceMM(buffer,mapword,file_nodes);//calling SegmentSentenceMM function Processes punctuation
		}	
		file_nodes++;
		node = node ->next;	
  	}
  	return file_nodes;
}


/*
*输出文件夹里文件的路径
*/
void DisFolderFilesName(struct FileList *node)
{
	while(node)
    	{
      		printf("%s\n",node->file_path);
      		node = node->next;
    	}
}



